package com.bing.my.controller;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.bing.common.config.Result;
import com.bing.my.entity.Bill;
import com.bing.my.entity.Goose;
import com.bing.my.entity.GooseBill;
import com.bing.my.entity.Member;
import com.bing.my.entity.vo.VoPay;
import com.bing.my.mapper.GooseMapper;
import com.bing.my.service.BillService;
import com.bing.my.service.GooseService;
import com.bing.my.service.MemberService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 账单数据 前端控制器
 * </p>
 *
 * @author bing
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private BillService billService;
    @Resource
    private GooseMapper gooseMapper;
    @Autowired
    private MemberService memberService;

    @RequiresAuthentication
    @PostMapping
    public Result<?> pay(@Validated @RequestBody VoPay voPay) {
        Bill bill = voPay.bill;
        ArrayList<GooseBill> gooseBill = voPay.gooseBill;

        if (billService.pay(bill.getMemberId(), bill, gooseBill)) {
            return Result.success();
        }
        return Result.error(503, "支付失败");
    }

    @RequiresAuthentication
    @GetMapping
    public Result<?> inquireBills(@RequestParam String star, @RequestParam String end) {
        List<VoPay> voPays = billService.inquirePayData(star, end);
        for (VoPay p : voPays) {
            ArrayList<GooseBill> gooseBill = p.getGooseBill();
            for (GooseBill g : gooseBill) {
                // 添加必要信息到gooseBill里
                Goose goose = gooseMapper.selectById(g.getGooseId());
                g.setGooseName(goose.getGooseName());
                g.setSellPrice(goose.getPrice());
                g.setStandard(goose.getStandard());
            }
            Bill bill = p.getBill();
            Member member = memberService.selectById(bill.getMemberId());
            bill.setMemberName(member.getMemberName());
        }
        return Result.success(voPays);
    }

}

